package butines.game2;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;

import butines.core.GameImage;

public class Drawer {

    private static Font font = new Font(Font.DIALOG, Font.PLAIN, 10);
    
    private Graphics2D g2d;
    private boolean fill;
    
    private int align = -1;
    
    private static Drawer instance = new Drawer();
    
    public static Drawer getInstance() {
        return instance;
    }
    
    public static void setGraphics(Graphics2D g2d) {
        instance.g2d = g2d;
    }
    
    public Drawer left() {
        align = -1;
        return this;
    }
    
    public Drawer center() {
        align = 0;
        return this;
    }
    
    public Drawer right() {
        align = 1;
        return this;
    }
    
    public void print(float sx, float sy, String str, Object ... args) {
        String str2 = String.format(str, args);
        if (align == 0) {
            sx -= g2d.getFontMetrics().stringWidth(str2) / 2;
        } else if (align == 1) {
            sx -= g2d.getFontMetrics().stringWidth(str2);
        }
        g2d.drawString(str2, Math.round(sx), Math.round(sy));
    }
    
    public Drawer color(int r, int g, int b, int a) {
        g2d.setColor(new Color(r, g, b, a));
        return this;
    }
    
    public Drawer color(Color c) {
        g2d.setColor(c);
        return this;
    }
    
    public Drawer defaultFont() {
        g2d.setFont(font);
        return this;
    }
    
    public Drawer black() {
        g2d.setColor(Color.BLACK);
        return this;
    }
    
    public Drawer white() {
        g2d.setColor(Color.WHITE);
        return this;
    }
    
    public Drawer red() {
        g2d.setColor(Color.RED);
        return this;
    }
    
    public Drawer fill() {
        fill = true;
        return this;
    }
    
    public Drawer draw() {
        fill = false;
        return this;
    }
    
    public Drawer circle(float sx, float sy, float r) {
        if (fill) {
            g2d.fillOval(Math.round(sx-r), Math.round(sy-r), Math.round(r*2), Math.round(r*2));
        } else {
            g2d.drawOval(Math.round(sx-r), Math.round(sy-r), Math.round(r*2), Math.round(r*2));
        }
        return this;
    }
    
    public Drawer oval(float sx, float sy, float w, float h) {
        if (fill) {
            g2d.fillOval(Math.round(sx), Math.round(sy), Math.round(w), Math.round(h));
        } else {
            g2d.drawOval(Math.round(sx), Math.round(sy), Math.round(w), Math.round(h));
        }
        return this;
    }
    
    public Drawer rect(float x, float y, float w, float h) {
        if (fill) {
            g2d.fillRect(Math.round(x), Math.round(y), Math.round(w), Math.round(h));
        } else {
            g2d.drawRect(Math.round(x), Math.round(y), Math.round(w), Math.round(h));
        }
        return this;
    }
    
    public Drawer square(float x, float y, float w) {
        rect(x, y, w, w);
        return this;
    }
    
    public Drawer line(float x1, float y1, float x2, float y2) {
        g2d.drawLine(Math.round(x1), Math.round(y1), Math.round(x2), Math.round(y2));
        return this;
    }
    
    public Drawer image(GameImage image, float x, float y) {
        if (image != null) {
            image.draw(g2d, x, y);
        }
        return this;        
    }
    
}
